/*
============================================================================
 Name        : BlockDPTJ.cpp
 Author      : \x{738b}\x{658c}
 Version     :
 Copyright   : \x{76ca}\x{76df}\x{8f6f}\x{4ef6}
 Description : CBlockDPTJ implementation
============================================================================
*/

#include "BlockDPTJ.h"
#include "eStockappui.h"
#include "Global.h"
#include "Field.h"
#include "Goods.h"
#include "BlockBargain.h"
#include "BlockPicHis.h"
#include "BlockPicCur.h"
#include <eStock_L2hy.rsg>
#include "eStockMainGridContainer.h"
#include "BlockMemo.h"

CBlockDPTJ::CBlockDPTJ(CBaseBlock* bsBack):
CBlockGoods(bsBack),
m_nStart(0)
{
    iOffset = 0;
    iHeight = m_nLineHeight;
}

CBlockDPTJ::~CBlockDPTJ()
{
	if(m_pstrFieldName != NULL)
		delete m_pstrFieldName, m_pstrFieldName = NULL;
	
    if (iFlickable != NULL)
        delete iFlickable, iFlickable = NULL;
}

CBlockDPTJ* CBlockDPTJ::NewLC(CBaseBlock* bsBack)
{
	CBlockDPTJ* self = new (ELeave)CBlockDPTJ(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockDPTJ* CBlockDPTJ::NewL(CBaseBlock* bsBack)
{
	CBlockDPTJ* self=CBlockDPTJ::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockDPTJ::ConstructL()
{
	CBlockGoods::ConstructL();
	m_bCanSetGoods = ETrue;
	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_MENU);

	m_pstrFieldName = new(ELeave)CDesCArrayFlat(8);
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_CGSZ);//\x{6301}\x{80a1}\x{5e02}\x{503c}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_ZLZJ);//\x{589e}\x{91cf}\x{8d44}\x{91d1}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_CGSL);//\x{6301}\x{80a1}\x{6570}\x{91cf}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_CGZJ);//\x{6301}\x{80a1}\x{589e}\x{51cf}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_ZLTG);//\x{5360}\x{6d41}\x{901a}\x{80a1}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_CCZJ);//\x{6301}\x{7b79}\x{589e}\x{51cf}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_CYJJ);//\x{6301}\x{6709}\x{57fa}\x{91d1}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_JJZJ);//\x{57fa}\x{91d1}\x{589e}\x{51cf}
	CGlobal::AddDesToArray(m_pstrFieldName,LOC_BUF_STR_GXRQ);//\x{66f4}\x{65b0}\x{65e5}\x{671f}

	m_nHelpType = MRTSHelp;

    iFlickable = CFlickable::NewL();
    iFlickable->SetAcceptMouseClick(this);
    iFlickable->SetThreshold(m_nLineHeight);
}

void CBlockDPTJ::AddTabs()
{
	TBuf<8> label;
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_DPTJ);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabDPTJ);
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_PROMPT2);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabMRTS);
	CGlobal::GetDesFromID(label, LOC_BUF_MENU_FINANCIAL2);
	label.Copy(label);
	iTabbar->AppendTab(label, KTabGSCW);
}

void CBlockDPTJ::OnDraw(CFbsBitGc& g)const
{
	TInt nField = m_pstrFieldName->Count();

	TField field;
	field.m_nGoodsID = m_goods.m_nGoodsID;
	field.m_nClose = m_goods.m_nClose;

	g.UseFont(m_pGlobal->g_font);

	if (m_bOutofTest)
	{
		g.SetPenColor(m_pGlobal->g_rgbText);
		g.DrawText(m_strOutofTest, TPoint(0, m_rect.iTl.iY + m_pGlobal->g_nFontHeight));
		return;
	}

	g.SetPenColor(m_pGlobal->g_rgbHighlight);

	TBuf<32> str;
	TRgb nColor;

    iStartItem = iOffset / iHeight;
    TInt y = iListRect.iTl.iY + iStartItem * iHeight - iOffset;
    if (iOffset <= 0)
    {
        iStartItem = 0;
        y = iListRect.iTl.iY;
    }
    iEndItem = iStartItem + iListRect.Height() / iHeight + 1;
    if (iEndItem > nField - 1)
        iEndItem = nField - 1;
    
    g.SetClippingRect(iListRect);
	for (TInt i = iStartItem; i < iEndItem; i++)
	{
		TInt nPos = iStartItem+i;
		if (nPos>=nField)
			break;
		
		str.Zero();
		TInt nY = /*m_rect.iTl.iY + m_nLineHeight * i*/y + m_pGlobal->m_nSpaceUp;

		g.SetPenColor(m_pGlobal->g_rgbText);
		g.DrawText((*m_pstrFieldName)[nPos], TPoint(0, nY+m_pGlobal->g_nFontHeight));

		if (nPos==0)
		{
			nColor = m_pGlobal->g_rgbVolume;
			CGlobal::Long2String(str,m_plDPTJ[nPos], 0);
			TBuf<2> temp;
			CGlobal::GetDesFromID(temp,LOC_BUF_STR_YUAN);
			str.Append(temp);
		}
		else if (nPos==2)
		{
			nColor = m_pGlobal->g_rgbAmount;
			CGlobal::Long2String(str,m_plDPTJ[nPos], 0);
			TBuf<2> temp;
			CGlobal::GetDesFromID(temp,LOC_BUF_STR_GU);
			str.Append(temp);
		}
		else if (nPos==1 || nPos==3)
		{
			if (m_plDPTJ[nPos]>=0)
				nColor = m_pGlobal->g_rgbRise;
			else
				nColor = m_pGlobal->g_rgbFall;
			CGlobal::Long2String(str,m_plDPTJ[nPos], 0);
			if (nPos==1)
			{
				TBuf<2> temp;
				CGlobal::GetDesFromID(temp,LOC_BUF_STR_YUAN);
				str.Append(temp);
			}
			else
			{
				TBuf<2> temp;
				CGlobal::GetDesFromID(temp,LOC_BUF_STR_GU);
				str.Append(temp);
			}
		}
		else if (nPos==4)
		{
			nColor = m_pGlobal->g_rgbVolume;
			str.AppendNum(m_plDPTJ[nPos]/100);
			str.Append('.');
			TBuf<16> temp;
#ifdef __SERIES60_3X__
			CGlobal::Int2String(temp,(TInt32)(m_plDPTJ[nPos]%100), 2);
#else
			TInt32 nTemp = CGlobal::Int64toInt32(m_plDPTJ[nPos]%100);
			CGlobal::Int2String(temp,(nTemp%100), 2);
#endif
			str.Append(temp);
			str.Append('%');
		}
		else if (nPos==5)
		{	
#ifdef __SERIES60_3X__
			TInt32 nValue = (TInt32)m_plDPTJ[nPos];
#else
			TInt32 nValue = CGlobal::Int64toInt32(m_plDPTJ[nPos]);
#endif
			if (nValue<-10000)
			{
				if (m_plDPTJ[2]>0)
				{
					nColor = m_pGlobal->g_rgbRise;
					CGlobal::GetDesFromID(str,LOC_BUF_STR_XINZENG);
				}
			}
			else
			{
				nColor = m_pGlobal->GetColor(nValue, 0);
				if (nValue>=0)
				{
#ifdef __SERIES60_3X__
					str.AppendNum(nValue/100);
#else
					str.AppendNum(m_plDPTJ[nPos]/100);
#endif
					str.Append('.');
					TBuf<16> temp;
					CGlobal::Int2String(temp,nValue%100, 2);
					str.Append(temp);
					str.Append('%');
				}
				else
				{
					str.Append('-');
#ifdef __SERIES60_3X__
					str.AppendNum(-nValue/100);
#else
					str.AppendNum(-m_plDPTJ[nPos]/100);
#endif
					str.Append('.');
					TBuf<16> temp;
					CGlobal::Int2String(temp,-nValue%100, 2);
					str.Append(temp);
					str.Append('%');
				}
			}
		}
		else if (nPos==6)
		{
			nColor = m_pGlobal->g_rgbAmount;
			str.AppendNum(m_plDPTJ[nPos]);
		}
		else if (nPos == 7)
		{
#ifdef __SERIES60_3X__
		TInt32 nValue = (TInt32)m_plDPTJ[nPos];
#else
		TInt32 nValue = CGlobal::Int64toInt32(m_plDPTJ[nPos]);
#endif
			nColor = m_pGlobal->GetColor(nValue, 0);
#ifdef __SERIES60_3X__
			str.AppendNum(nValue);
#else
			str.AppendNum(m_plDPTJ[nPos]);
#endif
		}
		else if (nPos == 8)
		{
#ifdef __SERIES60_3X__
			TInt32 nValue = (TInt32)m_plDPTJ[nPos];
#else
			TInt32 nValue = CGlobal::Int64toInt32(m_plDPTJ[nPos]);
#endif
			nColor = m_pGlobal->g_rgbNameCode;
			str.AppendNum(TInt(nValue/10000));
			str.Append(_L("\x5e74"));//\x{5e74}
			str.AppendNum(TInt(nValue%10000/100));
			str.Append(_L("\x6708"));//\x{6708}
			str.AppendNum(TInt(nValue%100));
			str.Append(_L("\x65e5"));//\x{65e5}
		}
		m_pGlobal->DrawString(g, str, m_rect.iBr.iX, nY,RIGHT, nColor, 0);
		
		y += iHeight;
	}
	g.CancelClippingRect();
}

void CBlockDPTJ::OnKeyDown(TInt keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
        TInt nLine = m_pstrFieldName->Count();
        TInt offset = iOffset;
        if (keyCode == m_pGlobal->g_nKeyUp)
        {
            offset -= iHeight;
            if (offset < 0)
                iTabbar->SetFocus(ETrue, m_pGlobal->g_nKeyUp);
        }
        else if (keyCode == m_pGlobal->g_nKeyDown)
        {
            if (offset >= iHeight * nLine - iListRect.Height())
                iToolbar->SetFocus(ETrue, m_pGlobal->g_nKeyDown);
            else
                offset += iHeight;
        }
        SetScrollOffset(TPoint(0, offset));
        DrawNow();
	}
	else
		CBlockGoods::OnKeyDown(keyCode);
}

void CBlockDPTJ::OnInput(const TDesC8& strInput)
{
	if (strInput.CompareF(_L8("01")) == 0)
	{
		CBlockBargain* bb = CBlockBargain::NewL(m_pBlockBack);
		bb->SetGoods(m_goods);
		GetContainer()->AddBlock(bb);
		return;
	}
	else if (strInput.CompareF(_L8("02")) == 0)
	{
		CBlockPicHis* bph = CBlockPicHis::NewL(m_pBlockBack);
		bph->SetGoods(m_goods);	
		GetContainer()->AddBlock(bph);
		return;
	}
	else if (strInput.CompareF(_L8("05")) == 0)
	{
		CBlockPicCur* bpc = CBlockPicCur::NewL(m_pBlockBack);
		bpc->SetGoods(m_goods);
		GetContainer()->AddBlock(bpc);
		return;
	}
	CBlock::OnInput(strInput);
}


TInt32 CBlockDPTJ::GetMenuItem()
{
	return 10;
}

void CBlockDPTJ::InitData()
{
	m_bOutofTest = EFalse;
	m_bSocketed = EFalse;

	TInt nField = m_pstrFieldName->Count();
	for (TInt i = 0; i<nField; i++)
		m_plDPTJ[i] = 0;

	m_nStart = 0;
}

TBool CBlockDPTJ::ReadData(const TDesC8& aData)
{
	TPtrC8 ptr(aData);
	TInt32 nGoodsID = CGlobal::ReadInt32L(ptr);

	if (nGoodsID==m_goods.m_nGoodsID)
	{
		TInt8 bStatus = CGlobal::ReadInt8L(ptr);
		if ((bStatus&128)!=0)
		{
			m_bOutofTest = ETrue;
			TInt16 sOp = CGlobal::ReadInt16L(ptr);
			HBufC* strPhone = CGlobal::ReadStringL(ptr);

			HBufC* buf = CGlobal::ReadStringL(ptr);
			m_strOutofTest.Copy(buf->Des());
			delete buf;
			m_bSocketed = ETrue;
			m_bExit = ETrue; //\x{8bd5}\x{7528}\x{671f}\x{8fc7}
			CBlockMemo* memo = CBlockMemo::NewL(m_pBlockBack,101,m_strOutofTest);
			memo->SetOperation(sOp,strPhone->Des());
			delete strPhone;
			GetContainer()->AddBlock(memo);
			return ETrue;
		}

		if ((bStatus&1)!=0)
		{
			HBufC* buf = CGlobal::ReadStringL(ptr);
			m_goods.m_strName.Copy(buf->Des());
			delete buf;
			m_pGlobal->AddGoods2ZJ(m_goods.m_nGoodsID, m_goods.m_strName);

			XInt32 x;
			x.SetRawData(CGlobal::ReadInt32L(ptr));
			m_plDPTJ[0] = x.GetValue();
			x.SetRawData(CGlobal::ReadInt32L(ptr));
			m_plDPTJ[1] = x.GetValue();
			x.SetRawData(CGlobal::ReadInt32L(ptr));
			m_plDPTJ[2] = x.GetValue();
			x.SetRawData(CGlobal::ReadInt32L(ptr));
			m_plDPTJ[3] = x.GetValue();

#ifdef __SERIES60_3X__
			m_plDPTJ[4] = CGlobal::ReadInt32L(ptr);
			m_plDPTJ[5] = CGlobal::ReadInt32L(ptr);
#else
			m_plDPTJ[4] = CGlobal::Int32toInt64(CGlobal::ReadInt32L(ptr));
			m_plDPTJ[5] = CGlobal::Int32toInt64(CGlobal::ReadInt32L(ptr));
#endif
			

			m_plDPTJ[6] = CGlobal::ReadInt16L(ptr);
			m_plDPTJ[7] = CGlobal::ReadInt16L(ptr);

#ifdef __SERIES60_3X__
			m_plDPTJ[8] = CGlobal::ReadInt32L(ptr);
#else
			m_plDPTJ[8] = CGlobal::Int32toInt64(CGlobal::ReadInt32L(ptr));
#endif
			

			m_bSocketed = ETrue;
			m_bExit = ETrue;    //\x{672c}\x{7c7b}\x{53ea}\x{6536}\x{4e00}\x{904d}\x{6570}\x{636e}
		}
	}
	return ETrue;
	
}

void CBlockDPTJ::WriteData(TDes8& aBuf)
{
	m_bOutofTest = EFalse;
	CGlobal::WriteInt32L(aBuf,m_goods.m_nGoodsID);
}

TInt CBlockDPTJ::GetDataLength()
{
	return 4;
}

TInt16 CBlockDPTJ::GetDataType()
{
	if (m_bSocketed)
		return RequestDataType_None;
	else
		return RequestDataType_DPTJ;
}

void CBlockDPTJ::ChangeScrollPos(TInt nPos)
{
	if (m_nStart!=nPos)
	{
		m_nStart = nPos;
		DrawNow();
	}
}

void CBlockDPTJ::SizeChanged()
{
	CBlockGoods::SizeChanged();
	iListRect = m_rect;
	SetScrollBar(0, m_pstrFieldName->Count());
	iTitlebar->FocusEnabled(ETrue);
	iTabbar->SetSelectedId(KTabDPTJ);
}

TPoint CBlockDPTJ::ScrollOffset() const
{
    return TPoint(0, iOffset);
}

void CBlockDPTJ::SetScrollOffset(const TPoint& aOffset)
{
    TInt y = aOffset.iY;
    if (y != iOffset)
    {
        iOffset = Max(0, Min(iHeight * m_pstrFieldName->Count() - iListRect.Height(), y));
        DrawNow();

//        if (m_bShowScroll)
//        {
//            
//            if (iStartItem == 0)
//                m_sbVert->SetPos(iStartItem);
//            else if (iEndItem == iTotalLines - 1)
//                m_sbVert->SetPos(iEndItem);
//            else
//                m_sbVert->SetPos((iStartItem + iEndItem) / 2);
//        }
    }
}

void CBlockDPTJ::OnMouseDown(const TPoint& aPt)
{
    iFlickable->HandleMousePress(aPt);
}

void CBlockDPTJ::OnMouseMove(const TPoint& aPt)
{
    iFlickable->HandleMouseMove(aPt);
}

void CBlockDPTJ::OnMouseUp(const TPoint& aPt)
{
    iFlickable->HandleMouseRelease(aPt);
}
